﻿'@Tác giả: Dương Văn Thiện
'@Tên file: FrmDonVi.vb
'@Ngày tạo: 05/01/2010
'@Ngày sửa: 05/01/2010
'@Mục đích: Danh mục đơn vị
'**************************
Imports System
Imports System.Drawing
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports DevExpress.XtraBars
Imports System.Linq
Imports DevExpress.XtraTreeList

Imports Publics.ModuleMethod
Imports Publics

Public Class FrmDonVi
    Private vSave_Load_ViewState As TreeListViewState
 
    Private _LaChonDV As Boolean
    Public Property LaChonDV() As Boolean
        Get
            Return _LaChonDV
        End Get
        Set(ByVal value As Boolean)
            _LaChonDV = value
        End Set
    End Property

    Private _objDonVi As DM_DonVi
    Public Property objDonVi() As DM_DonVi
        Get
            Return _objDonVi
        End Get
        Set(ByVal value As DM_DonVi)
            _objDonVi = value
        End Set
    End Property

    Public Shared Them, Sua, Xoa As Boolean
    Private Sub PhanQuyen()
        BtnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        BtnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        BtnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub

    Private Sub FrmDonVi_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.Text = "Danh mục đơn vị"
            FillDataToGrid()
            vTreeListDivision.Focus()
            vTreeListDivision.ExpandAll()
            btnChon.Visibility = IIf(LaChonDV = True, BarItemVisibility.Always, BarItemVisibility.Never)
            PhanQuyen()
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    'Lấy lại dữ liệu từ CSDL
    Private Sub FillDataToGrid()
        Try
            Dim strDonviQL = ""
            'Try
            '    strDonviQL = (From ds In dbTV.DM_BienMoiTruongs Where ds.InUsed _
            '                And ds.TenCauHinh = "vStrDonViSDQLNS" Select ds.GiaTriCauHinh).FirstOrDefault
            'Catch ex As Exception
            'End Try
            'Dim objDV = (From ds In dbTV.DM_DonVis Where ds.InUsed _
            '             And ds.MaDonVi = strDonviQL Select ds).FirstOrDefault
            'If objDV IsNot Nothing Then
            'Dim lst As List(Of String) = objDV.ID_Childs.Split(",").ToList
            vTreeListDivision.DataSource = From ds In dbTV.DM_DonVis _
                                       Where ds.InUsed _
                                       Order By ds.ThuTu, ds.TenDonVi _
                                       Select ds
            Me.vTreeListDivision.Refresh()
            'nd If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub vTreeListDivision_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles vTreeListDivision.MouseClick
        If e.Button = MouseButtons.Right Then
            Me.vPopupMenuDivision.ShowPopup(New Point(MousePosition.X, MousePosition.Y))
        End If
    End Sub

    Private Sub BtnXuatDuLieu_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnXuatDuLieu.ItemClick
        Dim TreeListExport As TreeList = Me.vTreeListDivision
        Export_Dialog(Me.vSaveFileDialog, TreeListExport)
    End Sub

    Private Sub vTreeListDivision_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles vTreeListDivision.KeyDown
        If Me.vTreeListDivision.FocusedNode IsNot Nothing Then
            If e.KeyCode = Keys.Right Then
                Me.vTreeListDivision.FocusedNode.Expanded = True
            ElseIf e.KeyCode = Keys.Left Then
                Me.vTreeListDivision.FocusedNode.Expanded = False
            ElseIf LaChonDV AndAlso e.KeyCode = Keys.Enter Then
                btnChon_ItemClick(Nothing, Nothing)
            ElseIf e.KeyCode = Keys.Enter Then
                Dim EditDivision As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                           Where ds.ID = CStr(vTreeListDivision.FocusedNode.GetValue(vGridColumnID)) _
                                           Select ds).FirstOrDefault()
                If EditDivision IsNot Nothing Then
                    Dim Frm As New FrmCapNhatDV
                    Frm.ActionForm = vEditAction
                    Frm.ObjDonVi = EditDivision
                    Frm.ShowDialog()
                    If Frm.IDDonVi <> "" Then
                        vSave_Load_ViewState = New TreeListViewState(vTreeListDivision)
                        vSave_Load_ViewState.SaveState()
                        FillDataToGrid()
                        vSave_Load_ViewState.LoadState()
                    End If
                End If
            End If
        End If
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Dim iHelpProvider As New HelpProvider
        'iHelpProvider.HelpNamespace = ".\HelpiPub.chm"
        'iHelpProvider.SetHelpNavigator(Me, HelpNavigator.KeywordIndex)
        'iHelpProvider.SetShowHelp(Me, True)
        'iHelpProvider.SetHelpKeyword(Me, "Danh muc don vi")
    End Sub

    Private Sub BtnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnThemMoi.ItemClick
        Dim Frm As New FrmCapNhatDV
        Frm.ActionForm = vAddAction
        Frm.ShowDialog()
        vSave_Load_ViewState = New TreeListViewState(vTreeListDivision)
        vSave_Load_ViewState.SaveState()
        FillDataToGrid()
        vSave_Load_ViewState.LoadState()
    End Sub

    Private Sub BtnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSuaDoi.ItemClick
        If vTreeListDivision.FocusedNode IsNot Nothing Then
            If Not String.IsNullOrEmpty(CStr(vTreeListDivision.FocusedNode.GetValue(vGridColumnID))) Then
                Dim EditDivision As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                                   Where ds.ID = CStr(vTreeListDivision.FocusedNode.GetValue(vGridColumnID)) _
                                                   Select ds).FirstOrDefault()
                If EditDivision IsNot Nothing Then
                    Dim Frm As New FrmCapNhatDV
                    Frm.ActionForm = vEditAction
                    Frm.ObjDonVi = EditDivision
                    Frm.ShowDialog()
                    If Frm.IDDonVi <> "" Then
                        vSave_Load_ViewState = New TreeListViewState(vTreeListDivision)
                        vSave_Load_ViewState.SaveState()
                        FillDataToGrid()
                        vSave_Load_ViewState.LoadState()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub BtnXoa_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnXoa.ItemClick
        If Me.vTreeListDivision.FocusedNode IsNot Nothing Then
            If Me.vTreeListDivision.FocusedNode.HasChildren Then
                'MsgBox(iGetTextFromResx("iUnDelParentDivision"), MsgBoxStyle.Information, iGetTextFromResx("iApplicationName"))
            Else
                Try
                    Dim ObjDonViXoa As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                                   Where ds.ID = CStr(vTreeListDivision.FocusedNode.GetValue(vGridColumnID)) _
                                                   Select ds).FirstOrDefault()
                    If ObjDonViXoa IsNot Nothing Then
                        Dim ExistUser As DM_NhanVien = (From ds In dbTV.DM_NhanViens _
                                                      Where ds.InUsed AndAlso ds.ID_DonVi = ObjDonViXoa.ID AndAlso ds.UserName IsNot Nothing _
                                                      Select ds).FirstOrDefault()
                        If ExistUser Is Nothing Then
                            If MsgAskDelete("Bạn có muốn xóa bản ghi này không?") = Windows.Forms.DialogResult.Yes Then
                                ObjDonViXoa.InUsed = False
                                ObjDonViXoa.EditedOn = CDate(dbTV.GetDateTimeServer())

                                iSubmitChange()
                                vSave_Load_ViewState = New TreeListViewState(vTreeListDivision)
                                vSave_Load_ViewState.SaveState()
                                FillDataToGrid()
                                vSave_Load_ViewState.LoadState()
                            End If
                        Else
                            MsgErr("Tồn tại nhân viên trong đơn vị")
                        End If
                    End If
                Catch ex As Exception
                    MsgErr(ex.Message)
                End Try
            End If
        End If
    End Sub

    Private Sub BtnLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnLamTuoi.ItemClick
        FillDataToGrid()
    End Sub

    Private Sub FrmDonVi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Thêm mới
        If (e.Modifiers = GetKeyModifide(pIdThem) And _
            e.KeyCode = GetKeyCode2(pIdThem)) Or _
            (e.KeyCode = GetKeyCode(pIdThem)) Then

            If Me.BtnThemMoi.Enabled = True And Me.BtnThemMoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnThemMoi_ItemClick(Nothing, Nothing)
            End If

        End If
        'Sửa
        If (e.Modifiers = GetKeyModifide(pIdSua) And _
           e.KeyCode = GetKeyCode2(pIdSua)) Or _
           (e.KeyCode = GetKeyCode(pIdSua)) Then

            If Me.BtnSuaDoi.Enabled = True And Me.BtnSuaDoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnSuaDoi_ItemClick(Nothing, Nothing)
            End If

        End If
        'Xoá
        If (e.Modifiers = GetKeyModifide(pIdXoa) And _
          e.KeyCode = GetKeyCode2(pIdXoa)) Or _
          (e.KeyCode = GetKeyCode(pIdXoa)) Then

            If Me.BtnXoa.Enabled = True And Me.BtnXoa.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnXoa_ItemClick(Nothing, Nothing)
            End If

        End If

        'Xuất dữ liệu
        If (e.Modifiers = GetKeyModifide(pIdXuat) And _
          e.KeyCode = GetKeyCode2(pIdXuat)) Or _
          (e.KeyCode = GetKeyCode(pIdXuat)) Then

            If Me.BtnXuatDuLieu.Enabled = True And Me.BtnXuatDuLieu.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnXuatDuLieu_ItemClick(Nothing, Nothing)
            End If

        End If
        'Làm tươi REFESH
        If (e.Modifiers = GetKeyModifide(pIdLamTuoi) And _
          e.KeyCode = GetKeyCode2(pIdLamTuoi)) Or _
          (e.KeyCode = GetKeyCode(pIdLamTuoi)) Then

            If Me.BtnLamTuoi.Enabled = True And Me.BtnLamTuoi.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnLamTuoi_ItemClick(Nothing, Nothing)
            End If

        End If

        'Huỷ bỏ (Thoát)
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
     e.KeyCode = GetKeyCode2(pIdThoat)) Or _
     (e.KeyCode = GetKeyCode(pIdThoat)) Then
            Me.Close()
        End If
    End Sub

    Private Sub btnChon_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnChon.ItemClick
        Try
            Dim id = CStr(vTreeListDivision.FocusedNode.GetValue(vGridColumnID))
            If id IsNot Nothing Then
                objDonVi = (From ds In dbTV.DM_DonVis Where ds.InUsed _
                            And ds.ID = id.Trim Select ds).FirstOrDefault
                Me.Close()
            End If
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub btnDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDong.ItemClick
        Me.Close()
    End Sub

     
End Class
